from rest_framework import serializers

from api import models
from django.db.models import F, Max


class AuctionModelSerializer(serializers.ModelSerializer):
    """拍卖场列表序列化器"""
    status_text = serializers.CharField(source='get_status_display')
    status = serializers.SerializerMethodField()
    cover = serializers.CharField()

    class Meta:
        model = models.Auction
        fields = ['id', 'status', 'status_text', 'title', 'cover', 'goods_count', 'look_count']

    def get_status(self, obj):
        """修改status状态"""
        status = obj.status
        if status == 1:
            status = ''
        elif status == 2:
            status = 'preview'
        elif status == 3:
            status = 'auction'
        elif status == 4:
            status = 'stop'
        return status


class AuctionItemModelSerializer(serializers.ModelSerializer):
    """单个拍卖场详细及商品列表"""
    preview_start_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
    preview_end_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
    auction_start_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
    auction_end_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
    status = serializers.CharField(source='get_status_display')
    cover = serializers.CharField()
    # 参与资格
    is_qualifications = serializers.SerializerMethodField()

    class Meta:
        model = models.Auction
        fields = ['id', 'status', 'title', 'cover', 'preview_start_time', 'preview_end_time', 'auction_start_time',
                  'auction_end_time', 'deposit', 'goods_count', 'bid_count', 'look_count', 'is_qualifications']

    def get_is_qualifications(self, obj):
        """曾经是否充值过"""
        # 查询到充值记录,获取最大的那个历史记录
        max_price = models.RechargeRecord.objects.filter(is_delete=False, is_show=True, status=2,
                                                         user=self.context['request'].user).aggregate(
            max_price=Max('amount'))['max_price']
        auction_deposit = obj.deposit

        # 如果历史充值大于保证金，&& 已经开拍
        if max_price > auction_deposit:
            if obj.status == 3:
                return True
        return False


class AuctionItemDetailModelSerializer(serializers.ModelSerializer):
    """拍品详细序列化器"""
    image = serializers.SerializerMethodField()
    item_detail = serializers.SerializerMethodField()

    class Meta:
        model = models.AuctionItem
        fields = ['id', 'uid', 'status', 'title', 'start_price', 'reserve_price', 'highest_price', 'unit', 'look_count',
                  'bid_count', 'image', 'item_detail']

    def get_image(self, obj):
        """拍品图片"""
        img_dic = models.AuctionItemImage.objects.filter(is_show=True, is_delete=False, item=obj).values('id', 'img')
        return img_dic

    def get_item_detail(self, obj):
        """拍品详细"""
        img_dic = models.AuctionItemDetail.objects.filter(is_show=True, is_delete=False, item=obj).values('id', 'key',
                                                                                                          'value')
        return img_dic
